﻿using BingVis.GoogleTransit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using BingVis.Common;
using System.Collections.Generic;
using System.Threading;

namespace BingVis.GoogleTransit.Test
{
    
    
    /// <summary>
    ///This is a test class for TransitRouteLoaderTest and is intended
    ///to contain all TransitRouteLoaderTest Unit Tests
    ///</summary>
    [TestClass()]
    public class TransitRouteLoaderTest
    {
        private string[] testRouteContents =
        {
            "route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color",
            "946_-1056,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            "946_-1058,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            "948_-1056,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            "948_-1058,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            "9462-1056,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            "Boat-F4,1,F4,Charlestown Ferry, ,4, , , ",
            "CR-Fairmount,1, ,Fairmount Line, ,2, ,8B118F,FFFFFF",
            "CR-Fitchburg,1, ,Fitchburg/South Acton Line, ,2, ,8B118F,FFFFFF",
            "CR-Worcester,1, ,Framingham/Worcester Line, ,2, ,8B118F,FFFFFF",
            "CR-Franklin,1, ,Franklin Line, ,2, ,8B118F,FFFFFF",
        };

        /// <summary>
        ///A test for TransitRouteLoader Constructor
        ///</summary>
        [TestMethod()]
        public void Should_Initialize_Values_Properly()
        {
            IGTFSFile file = new GTFSFile("routes.txt", testRouteContents);
            IEnumerable<ITransitTrip> trips = new List<ITransitTrip>();
            TransitRouteLoader target = new TransitRouteLoader(file, trips);

            Assert.IsNotNull(target.Items);
            Assert.IsNotNull(target.Items.Count);
        }

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_Items_Into_Routes()
        {
            IGTFSFile file = new GTFSFile("routes.txt", testRouteContents);
            IEnumerable<ITransitTrip> trips = new List<ITransitTrip>();
            TransitRouteLoader loader = new TransitRouteLoader(file, trips);
            int count = 0;
            bool done = false;

            loader.ItemLoaded += (x, y) => count++;
            loader.DoneProcessing += (x, y) => done = true;
            loader.ProcessFile();
            while(!done)
            { Thread.Sleep(200); }

            var target = loader.Items[0];

            //"route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color",
            //"946_-1056,1, ,Blue Line, ,1, ,2F5DA6,FFFFFF",
            Assert.AreEqual<string>("946_-1056", target.Id);
            Assert.AreEqual<string>("1", target.AgencyId);
            Assert.AreEqual<string>("", target.ShortName);
            Assert.AreEqual<string>("Blue Line", target.LongName);
            Assert.AreEqual<string>("", target.Description);
            Assert.AreEqual<int>(1, target.Type);
            Assert.AreEqual<string>("", target.Url);
            Assert.AreEqual<string>("2F5DA6", target.ColorHex);
        }

        /// <summary>
        ///A test for ProcessItem
        ///</summary>
        [TestMethod()]
        public void Should_Process_10_Items_From_TestContents()
        {
            IGTFSFile file = new GTFSFile("routes.txt", testRouteContents);
            IEnumerable<ITransitTrip> trips = new List<ITransitTrip>();
            TransitRouteLoader loader = new TransitRouteLoader(file, trips);
            int count = 0;
            bool done = false;

            loader.ItemLoaded += (x, y) => count++;
            loader.DoneProcessing += (x, y) => done = true;
            loader.ProcessFile();
            while (!done)
            { Thread.Sleep(200); }

            Assert.AreNotEqual<int>(0, loader.Items.Count);
            Assert.AreEqual<int>(10, count);
            Assert.AreEqual<int>(count, loader.Items.Count);
        }

    }
}
